package logic.med.scene
{
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.greensock.TweenAlign;
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import common.AFactory.AScene;
	import common.astar.AStar;
	import common.astar.Grid;
	import common.astar.Node;
	import common.avatar.AvatarSprite;
	import common.core.GEevnt;
	import common.manager.Avatar;
	import common.manager.GScene;
	import common.manager.GUI;
	import common.manager.ObserverEvt;
	import common.manager.Resource;
	import common.world.World;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import logic.avatar.Corpse;
	import logic.avatar.Pand;
	import logic.med.ui.MainBtn;
	
	public class SceneA extends AScene
	{
		public static const NAME : String = "SceneA";
		
		private var m_root : Sprite;
		
		private var m_pand : Pand;
		private var m_corpse : Corpse;
		
		private var m_mainBtn : MainBtn;
		
		private var m_avatars : Avatar;
		
		private var m_grid : Grid;
		private var m_astar : AStar;
		private var m_walkPath : Boolean;
		private var m_curAimNodPos : int;
		private var m_vx : Number;
		private var m_vy : Number;
		
		public function SceneA()
		{
			super();
		}
		
		public override function bulid(root : Sprite):void
		{
			m_root = root;
			
			m_mainBtn = new MainBtn;
			
			m_grid = new Grid(50,31);
			m_astar = new AStar;
			
			World.setBackgroundURL("Resources/SceneMap/scenemap_1.jpg","Resources/SceneMap/smallScenemap_1.png");

			World.worldHeight = 1000;
			World.worldWidth = 1600;
			
			GUI.open(MainBtn.NAME);
			
			var avatarClass : Object = new Object;
			avatarClass["Pand"] = Pand;
			avatarClass["Corpse"]= Corpse;
			
			m_avatars = new Avatar;
			m_avatars.build(avatarClass);
			
			m_pand = World.findEntity("0") as Pand;
			
			m_root.addEventListener(Event.ENTER_FRAME,onUpdate);
			
			ObserverEvt.addRec(World.EVENT_MOUSECLICK,this,OnClickMap);
		}
		
		private function OnClickMap(evt : GEevnt) : void
		{
			var mouseEvt : MouseEvent = evt.data;
			
			var pand : Pand = World.findEntity("0") as Pand;
			
			m_grid.setStartNode(pand.x/32,pand.y/32);
			m_grid.setEndNode(mouseEvt.localX/32,mouseEvt.localY/32);
			
			m_astar.findPath(m_grid);
			
			m_curAimNodPos = 0;
			
			m_walkPath = true;
		}
		
		private function onUpdate(evt : Event) : void
		{
			World.moveTo(m_pand.x,m_pand.y);
			
			if(m_walkPath)
			{
				m_walkPath = false;
				
				var node : Node = m_astar.path[m_curAimNodPos];
//				
				if(node)
				{
//					trace(node.x,"-",node.y);
					
//					var endX : Number = node.x * 32;
//					var endY : Number = node.y * 32;
//					
//					m_vx = (endX - m_pand.x)/8;
//					m_vy = (endY - m_pand.y)/8;
//					
//					if(m_pand.x != endX && m_pand.y != endY)
//					{
//						m_pand.x += m_vx;
//						m_pand.y += m_vy;
//					}
//					else
//					{
//						OnTweenComplete();
//					}
				
					TweenLite.to(m_pand,0.2,{x:node.x*32,y:node.y*32,ease:Linear.easeNone,onComplete:OnTweenComplete});
				}
				else
				{
					m_curAimNodPos = 0;
				}
			}
		}
		
		private function OnTweenComplete() : void
		{
			m_walkPath = true;
			m_curAimNodPos ++ ;
		}
		
		public override function destory():void
		{
			m_root.removeEventListener(Event.ENTER_FRAME,onUpdate);
			
			ObserverEvt.removeRec(World.EVENT_MOUSECLICK,this,OnClickMap);
			
			m_mainBtn.destory();
			m_pand.destory();
			
			m_mainBtn = null;
			m_pand = null;
			m_root = null;
		}	
	
	}
}